package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Description:test
 * @Create by: CAt
 * @Date;2021/9/2219:36
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String s = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());


        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);

        if (one == null) {
            return R.error("登录失败");
        }
        if (!one.getPassword().equals(s)) {
            return R.error("登录失败");
        }
        if (one.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        request.getSession().setAttribute("emloyee", one.getId());
        return R.success(one);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("emloyee");
        return R.success("退出成功");
    }

    @PostMapping
    public R add(@RequestBody Employee employee, HttpServletRequest request) {
        log.info("添加处理");
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String s) {
        IPage<Employee> pageInfor = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(s), Employee::getName, s);
        employeeService.page(pageInfor, queryWrapper);
        return R.success(pageInfor);
    }

//点击禁用启用??///
    @PutMapping
    public R jqyong(@RequestBody Employee employee, HttpServletRequest request){
        employee.setUpdateTime(LocalDateTime.now());

      employeeService.updateById(employee);
        return R.success("修改成功");
    }


    //编辑员工里的回显
    @GetMapping("/{id}")
    public R bianji(@PathVariable("id") Long zz){
        Employee byId = employeeService.getById(zz);
        return R.success(byId);
    }

}
